var survey_identifier;

$(function() {
        
        console.log('my Function');
	$("#InterviewInfoPage").on('pageinit', function(e) {
                 console.log('InterviewInfoPage create event');
                 $.ajax({
                        url: "http://m2.cip.gatech.edu/developer/wrecklist/widget/homeless_test/content/api/submit/1",
                        context: document.body,
                        dataType: "json", 
 		        type: 'GET', 
                        async: false,
                        success: function(data, textStatus, jqXHR){
                            $survey_identifier=data['surveyID'];
                            $("#identifier_i").html("<h4>Identifier: "+$survey_identifier+"</h4>");
                            console.log("Object returned by php:" + data);                         
                        },
                        error: function(jqHXR, textStatus, errorThrown) {
                            console.log('ajax error in get survey ID call:' +textStatus + ' ' + errorThrown);
                        }

                    }); // end of the ajax call

                navigator.geolocation.getCurrentPosition(onSuccess, onError,{ enableHighAccuracy: true });
              


        });
        
        //----------------------------
        $("#ParticipantInfoPage").on('pageinit', function(e) {
                 console.log('#ParticipantInfoPage create event');
                 $("#identifier_p").html("<h4>Identifier: "+$survey_identifier+"</h4>");

        });

 	//----------------------------
        $("#Q13").on('pageinit', function(e) {
                 console.log('#Q13');
                 $("#identifier_Q13").html("<h4>Identifier: "+$survey_identifier+"</h4>");

        });

 	//----------------------------
        $("#Q14-15").on('pageinit', function(e) {
                 console.log('#14-15');
                 $("#identifier_Q14-Q15").html("<h4>Identifier: "+$survey_identifier+"</h4>");

        });
 	//----------------------------
        $("#Q16").on('pageinit', function(e) {
                 console.log('#16');
                 $("#identifier_Q16").html("<h4>Identifier: "+$survey_identifier+"</h4>");

        });


});

function updateInterviewInfoPage(){
           var radioOption=$("input#radio1").attr ("checked");
	   var staff;
          // console.log(radioOption);
	   if(radioOption=="checked")
	      staff='Y';
	   else
	      staff='N';
           //console.log($survey_identifier+" "+$('#textinput5').val()+" "+$('#textinput6').val()+"  "+$('#textinput7').val()+"  "+$('#textinput1').val()+"  "+$('#textinput2').val()+"  "+staff);
           $.ajax({
		url: 'http://m2.cip.gatech.edu/developer/wrecklist/widget/homeless_test/content/api/submit/',
		context: document.body,
		cache: false,
		data: {'data':JSON.stringify({
                                ID:$survey_identifier,     
			        INTERVIEW_DATE:$('#textinput5').val()+" "+$('#textinput6').val(),
				INTERVIEW_LOCATION:$('#textinput7').val(),
				INTERVIEWER_NAME:$('#textinput1').val(),
 				AGENCY_NAME:$('#textinput2').val(),
				STAFF_OR_VOLUNTEER:staff
			})},
		type: 'POST',
		dataType: 'json',
		async: false,
		success: function(data, textStatus, jqXHR){
				console.log(data);
				
			}
	 });    

}

function updateParticipantInfoPage(){
           console.log("in participation info page");
	   var consentOption=$("input#radio4").attr ("checked");
	   var consent;
	   if(consentOption=="checked")
	      consent='1';
	   else
	      consent='0';
           $.ajax({
		url: 'http://m2.cip.gatech.edu/developer/wrecklist/widget/homeless_test/content/api/submit/',
		context: document.body,
		cache: false,
		data: {'data':JSON.stringify({
                                ID:$survey_identifier,     
			        LANGUAGE:$('#textinput8').val(),
				FIRST_NAME:$('#textinput9').val(),
				LAST_NAME:$('#textinput10').val(),
 				NICK_NAME:$('#textinput11').val(),
                                DATE_OF_BIRTH:$('#textinput12').val(),
				SSN:$('#textinput13').val(),
				CONSENT_TO_PARTICIPATE:consent
			})},
		type: 'POST',
		dataType: 'json',
		async: false,
		success: function(data, textStatus, jqXHR){
				console.log(data);
				
			}
	 });    

}

function updateQ13(){
	$.ajax({
		url: 'http://m2.cip.gatech.edu/developer/wrecklist/widget/homeless_test/content/api/submit/',
		context: document.body,
		cache: false,
		data: {'data':JSON.stringify({
                                ID:$survey_identifier,     
				YEARS_OF_HOMELESS:$('#textinput14').val(),
				MONTHS_OF_HOMELESS:$('#textinput15').val(),
 				
			})},
		type: 'POST',
		dataType: 'json',
		async: false,
		success: function(data, textStatus, jqXHR){
				console.log(data);
				
			}
	 });    

}

function updateQ14and15(){
         var sleep_most; 
         var sleep_most_other="N/A";
	 if($("input#radio6").attr("checked")=="checked")
	      sleep_most=$("input#radio6").val();
	 if($("input#radio7").attr("checked")=="checked")
	      sleep_most=$("input#radio7").val();
         if($("input#radio8").attr("checked")=="checked")
	      sleep_most=$("input#radio8").val();
         if($("input#radio9").attr("checked")=="checked")
	      sleep_most=$("input#radio9").val();
         if($("input#radio10").attr("checked")=="checked"){
	      sleep_most=$("input#radio10").val();
              sleep_most_other=$('#textinput17').val();
         }

	$.ajax({
		url: 'http://m2.cip.gatech.edu/developer/wrecklist/widget/homeless_test/content/api/submit/',
		context: document.body,
		cache: false,
		data: {'data':JSON.stringify({
                                ID:$survey_identifier,     
				TIMES_HOMELESS:$('#textinput16').val(),
				SLEEP_MOST:sleep_most,
   				SLEEP_MOST_IF_OTHER:sleep_most_other
 				
			})},
		type: 'POST',
		dataType: 'json',
		async: false,
		success: function(data, textStatus, jqXHR){
				console.log(data);
				
			}
	 });    
}

function updateQ16(){
 	 var prior_live; 
         var prior_live_other="N/A";
	 if($("input#radio11").attr("checked")=="checked")
	      prior_live=$("input#radio11").val();
	 if($("input#radio12").attr("checked")=="checked")
	      prior_live=$("input#radio12").val();
         if($("input#radio13").attr("checked")=="checked")
	      prior_live=$("input#radio13").val();
         if($("input#radio14").attr("checked")=="checked"){
	      prior_live=$("input#radio14").val();
              prior_live_other=$('#textinput18').val();
         }
         

	$.ajax({
		url: 'http://m2.cip.gatech.edu/developer/wrecklist/widget/homeless_test/content/api/submit/',
		context: document.body,
		cache: false,
		data: {'data':JSON.stringify({
                                ID:$survey_identifier,     
				PRIOR_LIVE:prior_live,
				PRIOR_LIVE_TOWNNAME:prior_live_other
 				
			})},
		type: 'POST',
		dataType: 'json',
		async: false,
		success: function(data, textStatus, jqXHR){
				console.log(data);
				
			}
	 });    

}

// onSuccess Geolocation      

function onSuccess(position) {

    var element = document.getElementById('map_canvas');

    element.innerHTML = 'Latitude: ' + position.coords.latitude + '<br />' +

                    'Longitude: ' + position.coords.longitude + '<br />' +

                    'Altitude: ' + position.coords.altitude + '<br />' +

                    'Accuracy: ' + position.coords.accuracy + '<br />' +

                    'Altitude Accuracy: ' + position.coords.altitudeAccuracy + '<br />' +

                    'Heading: ' + position.coords.heading + '<br />' +

                    'Speed: ' + position.coords.speed + '<br />' +

                    'Timestamp: ' + position.timestamp + '<br />';
      mobileDemo.center=position.coords.longitude +","+ position.coords.altitude ;

}

// onError Callback receives a PositionError object     

function onError(error) {

    alert('code: ' + error.code + '\n' +

      'message: ' + error.message + '\n');

}
